package com.st.listener;


import com.api.scenic.IScenicClient;
import com.api.tickets.ITicketsClient;
import com.st.entity.ScenicDetail;
import com.st.entity.Ticket;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

@Service
@RocketMQMessageListener(consumerGroup = "st-page-consumer",
        topic = "scenic-2-page",selectorExpression = "page-tag",
        consumeMode = ConsumeMode.CONCURRENTLY,
        messageModel = MessageModel.BROADCASTING)
public class PageListener implements RocketMQListener<Integer> {

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    @Autowired
    private ITicketsClient iTicketsClient;
    @Autowired
    private IScenicClient iScenicClient;


    @Override
    public void onMessage(Integer attractionsId) {
        ScenicDetail scenicDetail = iScenicClient.getScenicDetailByAid(attractionsId);
        int id = attractionsId;
        Ticket ticket = iTicketsClient.getTicketBy(id);
        scenicDetail.setTicket(ticket);
        //Ticket ticket = new Ticket();
        try {
            //生成静态化页面
            Configuration configuration = freeMarkerConfig.getConfiguration();
            Template template = configuration.getTemplate("introduction.ftl");
            Map data = new HashMap();
            data.put("scenicDetail",scenicDetail);
            //创建输出流(Writer)对象
            Writer out = new FileWriter("C:\\Users\\alan\\Desktop\\nginx-1.18.0\\scenic-page\\"+attractionsId+".html");
            //输出
            template.process(data,out);
            //关闭
            out.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}